package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.SpellgroupBean
import com.example.presentshopping.config.bean.SupplierListBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL.Req_getTeamGoodsList
import com.example.presentshopping.ui.custom.RoundImageView
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_experience.*
import kotlinx.android.synthetic.main.activity_ordermly.*
import kotlinx.android.synthetic.main.activity_ordermly.title_main_tv
import kotlinx.android.synthetic.main.activity_spellgroup.*
import kotlinx.android.synthetic.main.activity_spellgroup.recycler_view
import kotlinx.android.synthetic.main.activity_spellgroup.refresh_layout

//立即拼团
class SpellgroupActivity : BaseMvpActivity() {

    var spellgroupBran: SpellgroupBean? = null
    var baseRecyclerAdapter: BaseRecyclerAdapter<SpellgroupBean.DataBean.ListBean>? = null
    private var queryInfoPresenter: PublicPresenter? = null

    var page = 1

    override val layoutId: Int
        get() = R.layout.activity_spellgroup

    override fun initView(savedInstanceState: Bundle?) {
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        queryInfoPresenter = PublicPresenter(this, false, "")
        queryInfoPresenter?.attachView(this)
        setAdapter()
        queryInfoenter(page)

        refresh_layout.setOnRefreshListener {
            page = 1
            queryInfoenter(page)  }
        recycler_view?.setLoadMoreListener {
            page++
            queryInfoenter(page)
        }



    }



    override fun showToast(msg: String?) {
      ToastUtil.showMsg(msg)
    }

    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        super.onDestroy()
    }

    override fun onSuccess(data: String?) {
        refresh_layout?.isRefreshing = false
        recycler_view.loadMoreFinish(false, true)
        spellgroupBran =GsonUtils.getGson().fromJson(data, SpellgroupBean().javaClass)
        if (page==1){
            baseRecyclerAdapter?.clear()
        }
        if(spellgroupBran?.data?.list?.size!! >0){
            recycler_view.loadMoreFinish(false, true)
            baseRecyclerAdapter?.setData(spellgroupBran?.data?.list)
        }else{
            recycler_view.loadMoreFinish(true, false)
        }

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }
   fun setAdapter(){

       // 使用默认的加载更多的View。
       recycler_view.useDefaultLoadMore()
       recycler_view.loadMoreFinish(false, true)

       baseRecyclerAdapter =object :BaseRecyclerAdapter<SpellgroupBean.DataBean.ListBean>(this,null,R.layout.item_discover){
           override fun convert(
               holder: BaseRecyclerHolder?,
               data: SpellgroupBean.DataBean.ListBean,
               position: Int,
               isScrolling: Boolean,
               selectedPosition: Int
           ) {
               val ivimage = holder?.getView<RoundImageView>(R.id.iv_image)
               val tv_name =holder?.getView<TextView>(R.id.tv_name)
               val tv_description =holder?.getView<TextView>(R.id.tv_description)
               val myodtunbsws =holder?.getView<LinearLayout>(R.id.myodtunbsws)

               tv_name?.text =data.title
               tv_description?.text = "¥: "+data.marketprice

              GlideUtil.load(context,data.thumb,ivimage)
               myodtunbsws?.setOnClickListener {
                   val Init = Intent(context, DetailsActivity().javaClass)

                   Init.putExtra("msid",data.id.toString())
                   Init.putExtra("Activity","SpellgroupActivity")
                   context?.startActivity(Init)
                   finish()
               }

           }

       }
       recycler_view.adapter =baseRecyclerAdapter
   }



    fun queryInfoenter(pata:Int) {
        queryInfoPresenter?.netWorkRequestPost(
            Req_getTeamGoodsList,
            HttpRequest.getTeamGoodsList(1, pata, 10)
        )
    }
}